package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.Images;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.ImagesMapper;
import com.lyy.photovoltaic.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Service
public class ImagesServiceImpl implements ImagesService {
    @Autowired
    ImagesMapper imagesMapper;
    @Override
    public ResultVO uploadImages(MultipartFile files) {
        //1.使用MultipartFile接收前端的文件
        //2.获取文件名字
        System.out.println(files);
        if (files==null) {
            return null;
        }
        String fileName = files.getOriginalFilename();
        System.out.println(fileName);
        //设置文件存储路径
        String path = "F:\\HBuildr\\VUE\\photovoltaic\\src\\assets\\equipments";
        File newPath = new File(path);
        System.out.println(path);
        if (!newPath.exists()) {
            newPath.mkdirs();
        }
        try {
            files.transferTo(new File(newPath, fileName));
            return new ResultVO(ResultCode.SUCCESS,"图片上传成功",fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO(ResultCode.FAILED,"图片上传失败",null);
        }
    }

    @Override
    public ResultVO insertImage(String imageUrl) {
        //将图片存在本地路径，并在数据库中存储图片名称
        Images images = new Images();
        images.setImageUrl(imageUrl);
        int i = imagesMapper.insert(images);
        System.out.println(i);
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "插入图片成功",imageUrl);
        return resultVO;
    }

    @Override
    public ResultVO uoloadTouxaing(MultipartFile files) {
        //1.使用MultipartFile接收前端的文件
        //2.获取文件名字
        System.out.println(files);
        if (files==null) {
            return null;
        }
        String fileName = files.getOriginalFilename();
        //System.out.println(fileName);
        //设置文件存储路径
        String path = "F:\\HBuildr\\VUE\\photovoltaic\\src\\assets\\touxiang";
        File newPath = new File(path);
        System.out.println(path);
        if (!newPath.exists()) {
            newPath.mkdirs();
        }
        try {
            files.transferTo(new File(newPath, fileName));
            return new ResultVO(ResultCode.SUCCESS,"头像上传成功",fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO(ResultCode.FAILED,"头像上传失败",null);
        }
    }

    @Override
    public ResultVO uploadNoticeImages(MultipartFile files) {
        System.out.println(files);
        if (files==null) {
            return null;
        }
        String fileName = files.getOriginalFilename();
        //System.out.println(fileName);
        //设置文件存储路径
        String path = "F:\\HBuildr\\VUE\\photovoltaic\\src\\assets\\notice";
        File newPath = new File(path);
        System.out.println(path);
        if (!newPath.exists()) {
            newPath.mkdirs();
        }
        try {
            files.transferTo(new File(newPath, fileName));
            return new ResultVO(ResultCode.SUCCESS,"图片上传成功",fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO(ResultCode.FAILED,"图片上传失败",null);
        }
    }

    @Override
    public ResultVO uploadStaffsImages(MultipartFile files) {
        System.out.println(files);
        if (files==null) {
            return null;
        }
        String fileName = files.getOriginalFilename();
        //System.out.println(fileName);
        //设置文件存储路径
        String path = "F:\\HBuildr\\VUE\\photovoltaic\\src\\assets\\staffs";
        File newPath = new File(path);
        System.out.println(path);
        if (!newPath.exists()) {
            newPath.mkdirs();
        }
        try {
            files.transferTo(new File(newPath, fileName));
            return new ResultVO(ResultCode.SUCCESS,"图片上传成功",fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO(ResultCode.FAILED,"图片上传失败",null);
        }
    }
}
